package group6renderer;
import group6renderer.renderernode.*;

import java.util.HashMap;

/**
 * @author Yiannis
 * A class to track all of the constants in the program such as the tag strings and escaped characters, and to return instances of the node classes when passed a tag string
 * Collaborators: Renderer, Display, Parser
 */
public class HTMLConstants 
{

    public static TreeNode currentBasicNode;	
    private static HashMap<String, TreeNode> constants;
    static private int COUNTER_HTML;
    static private int COUNTER_HEAD;
    static private int COUNTER_TITLE;
    static private int COUNTER_BODY;
    
    
    public static final String S_COPY="�";
    public static final String S_AMP="&";
    public static final String S_QUOT="\"";
    public static final String S_REG="�"; 
    public static final String S_POUND="�";
    public static final String S_NBSP=" ";
    public static final String S_FRAC12="�";
    public static final String S_EUML="�";
    public static final String S_EACUTE="�";
    public static final String S_EGRAVE="�";
    public static final String S_MICRO="�";
    public static final String S_SUP2="�";

    
    public static final String S_COPY_EXP="&copy;";
    public static final String S_AMP_EXP="&amp;";
    public static final String S_QUOT_EXP="&quot;";
    public static final String S_REG_EXP="&reg;"; 
    public static final String S_POUND_EXP="&pound;";
    public static final String S_NBSP_EXP="&nbsp;";
    public static final String S_FRAC12_EXP="&frac12;";
    public static final String S_EGRAVE_EXP="&egrave;";
    public static final String S_EACUTE_EXP="&eacute;";
    public static final String S_EUML_EXP="&euml;";
    public static final String S_MICRO_EXP="&micro;";
    public static final String S_SUP2_EXP="&sup2;";

    
    
    public static final String BOLD = "b";
    public static final String ITALIC = "i";
    public static final String UNDERLINE = "u";
    public static final String WRAPPED = "w";
    public static final String BLOCKQUOTE = "blockquote";
    public static final String PRE = "pre";
    public static final String TTFONT="tt";
    
   
    public static final String CENTER = "center";
    public static final String RIGHT = "right";
    public static final String LEFT = "left";
   
    public static final String HTML = "html";
    public static final String HEAD = "head";
    public static final String TITLE = "title";
    public static final String BODY = "body";
    
    public static final String HEADING = "h";
    public static final String HEADING1 = "h1";
    public static final String HEADING2 = "h2";
    public static final String HEADING3 = "h3";
    public static final String HEADING4 = "h4";
    public static final String HEADING5 = "h5";
    public static final String HEADING6 = "h6";
    public static final String HEADING7 = "h7";
   
    public static final String DEFINITION_LIST = "dl"; 
    public static final String DEFINITION_TITLE = "dt";
    public static final String DEFINITION_DATA = "dd";
     
    
    
    public static final String ORDLIST = "ol";
    public static final String UNORDLIST ="ul";
    public static final String LISTINDEX ="li";
    
    public static final String PARAGRAPH ="p";
    public static final String BREAK ="br";
    public static final String IMAGE = "img";
    public static final String HORIZONTALRULE = "hr";
    
    public static final String TABLE = "table";
    public static final String TABLEROW = "tr";
    public static final String TABLECELL = "td";
    
    
    public static final int LEVELS_OF_HEADINGS = 7;
    
    /*
     * Initialises the constants hashmap
     */
    public static void buildHTMLConstants()
    {
	constants = new HashMap<String, TreeNode>();
	
	constants.put(HTML, new HtmlNode());
	constants.put(HEAD, new HeadNode());
	constants.put(TITLE, new TitleNode(TITLE));
	constants.put(BODY, new BodyNode());
	
	
	constants.put(BOLD, new BoldNode(BOLD));
	constants.put(ITALIC, new ItalicNode(ITALIC));
	constants.put(UNDERLINE, new UnderLineNode(UNDERLINE));
	constants.put(BLOCKQUOTE, new BlockQuoteNode(BLOCKQUOTE));
	constants.put(PRE, new PreNode(PRE));
	constants.put(TTFONT, new TTFontNode(TTFONT));
	
	
	constants.put(CENTER, new AlignmentNode(CENTER));
	constants.put(RIGHT, new AlignmentNode(RIGHT));
	constants.put(LEFT, new AlignmentNode(LEFT));
	
	constants.put(HEADING1, new HeadingNode(HEADING1));
	constants.put(HEADING2, new HeadingNode(HEADING2));
	constants.put(HEADING3, new HeadingNode(HEADING3));
	constants.put(HEADING4, new HeadingNode(HEADING4));
	constants.put(HEADING5, new HeadingNode(HEADING5));
	constants.put(HEADING6, new HeadingNode(HEADING6));
	constants.put(HEADING7, new HeadingNode(HEADING7));
	
	constants.put(PARAGRAPH, new ParagraphNode(PARAGRAPH));
	constants.put(BREAK, new BreakNode(BREAK));
	
	constants.put(ORDLIST, new ListNode(ORDLIST));
	constants.put(UNORDLIST, new ListNode(UNORDLIST));
	constants.put(LISTINDEX, new ListIndexNode(LISTINDEX));
	
	constants.put(DEFINITION_LIST, new DefinitionListNode(DEFINITION_LIST));
	constants.put(DEFINITION_TITLE, new DefinitionTitleNode(DEFINITION_TITLE));
	constants.put(DEFINITION_DATA, new DefinitionDataNode(DEFINITION_DATA));
	
	
	constants.put(TABLE, new TableNode(TABLE));
	constants.put(TABLEROW, new TableRowNode(TABLEROW));
	constants.put(TABLECELL, new TableCellNode(TABLECELL));
	constants.put(IMAGE, new ImageNode());
	constants.put(HORIZONTALRULE, new HorizontalRuleNode(HORIZONTALRULE));
    }
    
    /**
     * Tests if a tag is an opening tag
     * @param the tag to be tested
     * @return false if it is a closing tag, true if it is an opening tag
     */
    
    public static boolean isOpeningTag(String tag)
    {
	return tag.contains("/");
    }
    
    /**
     * Gets a new tag object from the constants hashmap according to the tag string passed in.
     * @param The tag string
     * @return An instance of the correct tag class
     * @throws java.lang.Exception
     */
    public static TreeNode getInstance(String tag)throws Exception
    {


    	if (!constants.containsKey(tag)) 
    		throw new Exception("Unknown tag.");
    	
    	
    	TreeNode node= constants.get(tag).copy();
    	node.getName();

	
	if (node instanceof HtmlNode) 
		{
			COUNTER_HTML++;
			if (COUNTER_HTML>1)
			throw new Exception("Only 1 html tag is allowed and only in the beginning of the text and is already been created even from the user even from the program.");
			currentBasicNode=node;
		}
	else
	if (node instanceof HeadNode) 
	{
		COUNTER_HEAD++;
		if (COUNTER_HEAD>1)
		throw new Exception("Only 1 head tag is allowed and is already been created even from the user even from the program");
	}
	else
	if (node instanceof TitleNode) 
	{
		COUNTER_TITLE++;
		if (COUNTER_TITLE>1)
		throw new Exception("Only 1 title tag is allowed and is already been created.");
	}
	else
	if (node instanceof BodyNode) 
	{
		COUNTER_BODY++;
		if (COUNTER_BODY>1)
		throw new Exception("Only 1 body tag is allowed and is already been created even from the user even from the program");
		currentBasicNode=node;
	}
	
	return node;
	}

    
    
}
